package org.jeecg.modules.demo.mall.yxstoreproduct.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.mall.entity.YxMerchantStore;
import org.jeecg.modules.demo.mall.entity.YxShopSku;
import org.jeecg.modules.demo.mall.entity.YxStoreProduct;
import org.jeecg.modules.demo.mall.yxmerchantstore.mapper.YxMerchantStoreMapper;
import org.jeecg.modules.demo.mall.yxshopproduct.mapper.YxShopProductMapper;
import org.jeecg.modules.demo.mall.yxshopsku.mapper.YxShopSkuMapper;
import org.jeecg.modules.demo.mall.yxstoreproduct.mapper.YxStoreProductMapper;
import org.jeecg.modules.demo.mall.yxstoreproduct.service.IYxStoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: yx_store_product
 * @Author: jeecg-boot
 * @Date: 2023-10-19
 * @Version: V1.0
 */
@Service
public class YxStoreProductServiceImpl extends ServiceImpl<YxStoreProductMapper, YxStoreProduct> implements IYxStoreProductService {

    @Resource
    private YxStoreProductMapper yxStoreProductMapper;
    @Resource
    private YxShopProductMapper yxShopProductMapper;
    @Autowired
    private YxMerchantStoreMapper yxMerchantStoreMapper;
    @Resource
    private YxShopSkuMapper yxShopSkuMapper;

    @Override
    public Integer sort() {
        return yxStoreProductMapper.sort();
    }


    @Override
    public void updateNum(Integer productId, Integer num, Integer productType, Integer orderPayType, Integer skuId,String merStoreId) {
        //平台商品
        if (productType != null && productType.equals(0)) {
            YxMerchantStore yxStoreProduct = yxMerchantStoreMapper.selectById(merStoreId);
            if (yxStoreProduct != null) {
                //购买
                if (orderPayType != null && orderPayType.equals(1)) {
                    yxStoreProduct.setStock(yxStoreProduct.getStock() - num);
                    yxStoreProduct.setSales(yxStoreProduct.getSales() + num);
                }
                //退款
                if (orderPayType != null && orderPayType.equals(2)) {
                    yxStoreProduct.setStock(yxStoreProduct.getStock() + num);
                    yxStoreProduct.setSales(yxStoreProduct.getSales() - num);
                }
                yxMerchantStoreMapper.updateById(yxStoreProduct);

            }
        }
        //商家商品
        if (productType != null && productType.equals(1) && skuId != null) {
            YxShopSku yxShopSku = yxShopSkuMapper.selectById(skuId);
            if (yxShopSku != null) {
                if (orderPayType != null && orderPayType.equals(1)) {
                    yxShopSku.setStock(yxShopSku.getStock() - num);
                    yxShopSku.setSales(yxShopSku.getSales() + num);
                }
                //退款
                if (orderPayType != null && orderPayType.equals(2)) {
                    yxShopSku.setStock(yxShopSku.getStock() + num);
                    yxShopSku.setSales(yxShopSku.getSales() - num);
                }
                yxShopSkuMapper.updateById(yxShopSku);
            }
        }
    }

    @Override
    public Boolean checkNum(Integer productId, Integer num, Integer productType, Integer orderPayType, Integer skuId,String merStoreId) {
        //平台商品
        if (productType != null && productType.equals(0)) {
            YxMerchantStore yxStoreProduct = yxMerchantStoreMapper.selectById(merStoreId);
            if (yxStoreProduct != null) {
                //判断库存
                if (yxStoreProduct.getStock() < num) {
                    return Boolean.FALSE;
                }
            }
        }
        //商家商品
        if (productType != null && productType.equals(1) && skuId != null) {
            YxShopSku yxShopSku = yxShopSkuMapper.selectById(skuId);
            if (yxShopSku != null) {
                if (yxShopSku.getStock() < num) {
                    return Boolean.FALSE;
                }
            }
        }
        return Boolean.TRUE;
    }
}
